<?php

namespace app\modules\admin\controllers;

use app\models\Admin;
use app\models\AssignUser;
use yii;
use app\models\LoginForm;
use app\models\ChangePassword;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
/**
 * Default controller for the `admin` module
 */
class DefaultController extends Controller
{
    /**
     * Renders the index view for the module
     * @return string
     */

    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['logout', 'index', 'change-password','assign-user'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
//            'captcha' => [
//                'class' => 'yii\captcha\CaptchaAction',
//                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
//            ],
        ];
    }

    public function actionIndex()
    {
//        $auth = Yii::$app->authManager;
//        $pChangePassword = $auth->createPermission('site/change-password');
//        $rSuperAdmin = $auth->createRole('superAdmin');
//        $auth->add($pChangePassword);
//        $auth->add($rSuperAdmin);
//        $auth->addChild($rSuperAdmin,$pChangePassword);
//        $auth->assign($rSuperAdmin,Yii::$app->user->id);

        return $this->render('index');
    }

    public function actionLogin()
    {
        $this->layout = 'login.php';
        if (!\Yii::$app->user->isGuest) {
            return $this->redirect(['/admin/default']);
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->redirect(['/admin/default']);
        }
        return $this->render('login', [
            'model' => $model,
        ]);
    }

    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->redirect(['/admin']);
    }

    public function actionChangePassword()
    {

        if(!Yii::$app->user->can('site/change-password')){
            throw new yii\web\ForbiddenHttpException('没有权限');
        }
        $model = new ChangePassword();

        if ($model->loadPost() && $model->validate()) {

            if ($model->save()) {
                Yii::$app->session->setFlash('success', '修改成功！');
            }
            return $this->refresh();
        }

        return $this->render('change-password', [
            'model' => $model,
        ]);
    }

    public function actionAssignUser()
    {
        $model = new AssignUser();
        if ($model->loadPost()) {

            if ($model->save()) {
                Yii::$app->session->setFlash('success', '创建成功！');
            }
            return $this->refresh();
        }
        return $this->render('assign-user', [
            'model' => $model,
        ]);
    }
}
